package send.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.init.ConfigInitial;
import com.init.SingletonCache;
import com.util.LogUtil;
import com.util.PagedRequest;
import com.util.PagedResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import send.model.News;
import send.service.NewsService;
import send.weixin.model.request.TextMessage;
import send.weixin.service.WeChatService;
import send.weixin.util.MessageUtil;
import send.weixin.util.WeChatMsgUtil;
import sys.model.Questionnaire;
import sys.service.QuestionnaireService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;


/**
 *
 *
 * @author leson
 * @see 新闻接口的相关接入工作。
 */
@Controller
@RequestMapping("/news")
public class NewsController {
    @Autowired
    private NewsService newsService;
    /**
     * 新闻接入
     */
    @RequestMapping(value="/getxy_news",method = { RequestMethod.POST,
            RequestMethod.GET },produces = "text/plain;charset=UTF-8")
    @ResponseBody
    public String  get(HttpServletRequest request,HttpServletResponse
            response) throws IOException {
        LogUtil.info("获取校园新闻====>");
        List<News> list=newsService.getXYNewsNow();
        return JSON.toJSONString(list);

    }

    /**
     * 新闻接入
     */
    @RequestMapping(value="/getNewsView",method = { RequestMethod.POST,
            RequestMethod.GET },produces = "text/plain;charset=UTF-8")
    @ResponseBody
    public String  getNewsView(PagedRequest pagedRequest, HttpServletRequest request, HttpServletResponse
            response) throws IOException {
        LogUtil.info("获取新闻列表====>");
        List<News> list=newsService.getXYNewsNow();
        PagedResult<News> pagedResult =new PagedResult<>();
        pagedResult.setDataList(list);
        if(list!=null){
        pagedResult.setRows(list.size());
        pagedResult.setRecord(list.size());}
        else{
            pagedResult.setRows(0);
            pagedResult.setRecord(0);
        }
        pagedResult.setPage(1);
        pagedResult.setTotal(1);
        return JSON.toJSONString(pagedResult);

    }


    /**
     * 新闻接入
     */
    @RequestMapping(value="/addNews",method = { RequestMethod.POST,
            RequestMethod.GET },produces = "text/plain;charset=UTF-8")
    @ResponseBody
    public String  addNews(PagedRequest pagedRequest, HttpServletRequest request, HttpServletResponse
            response,int id,String title,String brief,String link) throws IOException {
        LogUtil.info("添加新闻====>");
        JSONObject json=new JSONObject();
        News news =new  News(id,title,brief,link) ;
        List<News> list= (List<News>) SingletonCache.getCacheElement(ConfigInitial.News,SingletonCache.getCache(ConfigInitial.Init));
        if(list==null){
            list=new ArrayList<News>() ;
             SingletonCache.addToCache(ConfigInitial.News,list,SingletonCache.getCache(ConfigInitial.Init));
        }
        list.add(news);
        json.put("status","true");
        json.put("msg","添加成功");
        return  json.toString();
    }



    /**
     * 修改
     */
    @RequestMapping(value="/editNews",method = { RequestMethod.POST,
            RequestMethod.GET },produces = "text/plain;charset=UTF-8")
    @ResponseBody
    public String  editNews(PagedRequest pagedRequest, HttpServletRequest request, HttpServletResponse
            response,int id,String title,String brief,String link) throws IOException {
        LogUtil.info("修改新闻====>");
        JSONObject json=new JSONObject();
        News news =new  News(id,title,brief,link) ;
        List<News> list= (List<News>) SingletonCache.getCacheElement(ConfigInitial.News,SingletonCache.getCache(ConfigInitial.Init));
        if(list==null){
            list=new ArrayList<News>() ;
            list.add(news);
            SingletonCache.addToCache(ConfigInitial.News,list,SingletonCache.getCache(ConfigInitial.Init));
        }
        else{
            for (News news1 : list) {
                if(news1.getId().equals(news.getId())){
                    list.remove(news1);
                    list.add(news);
                    break;
                }
            }
        }

        json.put("status","true");
        json.put("msg","修改成功");
        return  json.toString();
    }

    /**
     * 删除
     */
    @RequestMapping(value="/deleteNews",method = { RequestMethod.POST,
            RequestMethod.GET },produces = "text/plain;charset=UTF-8")
    @ResponseBody
    public String  deleteNews(PagedRequest pagedRequest, HttpServletRequest request, HttpServletResponse
            response,int id) throws IOException {
        LogUtil.info("删除新闻====>");
        JSONObject json=new JSONObject();
        List<News> list= (List<News>) SingletonCache.getCacheElement(ConfigInitial.News,SingletonCache.getCache(ConfigInitial.Init));
        if(list==null){
            list=new ArrayList<News>() ;
            SingletonCache.addToCache(ConfigInitial.News,list,SingletonCache.getCache(ConfigInitial.Init));
        }
        else{
            for (News news1 : list) {
                if(news1.getId().equals(id)){
                    list.remove(news1);
                    break;
                }
            }
        }

        json.put("status","true");
        json.put("msg","删除成功");
        return  json.toString();
    }

    }
